package com.scarecrow.olga.util {
	import com.vostu.worldedit.operations.Log;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class CFGReader{
		
		public function CFGReader() {
			
		}
		public function read(data:String):Object {
			
			var configMap:Object = new Object();
			
			var statements:Array = data.split("\r\n");
			
			for each(var statement:String in statements) {
				
				if (statement.length < 3) continue;
				//if comment, skip statement.
				if (statement.indexOf("//") == 0 || statement == "") continue;
				//trim all spaces
				statement = statement.split(" ").join();
				//separate key from value
				var keyArg:Array = statement.split("=");
				if (keyArg.length < 2) continue;
				//erase inline comments.
				keyArg[1] = keyArg[1].split("//")[0];
				
				if (keyArg[1].indexOf("[") != -1) {
					configMap[keyArg[0]] = readArray(keyArg[1]);
				} else {
					configMap[keyArg[0]] = keyArg[1];
				}
			}
			
			return configMap;
		}
		private function readArray(statement:String):Array {
			statement = statement.replace("[", "");
			statement = statement.replace("]", "");
			return statement.split(",");
		}
	}

}